package com.cloud.easy.im.utils;

import io.netty.channel.Channel;
import io.netty.util.CharsetUtil;

import java.io.UnsupportedEncodingException;
import java.net.SocketAddress;

/**
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月03日6:46 下午
 */
public class CharsetHelper {

    public final static String ENCODE_CHARSET = CharsetUtil.UTF_8.name();

    /**
     * 将session转为string的结构化输出
     *
     * @param session
     * @return
     */
    public static String sessionToString(Channel session) {

        SocketAddress remoteAddress = session.remoteAddress();
        //TODO 后续还需返回userId以便进一步确认
        return remoteAddress.toString();

    }

    public static byte[] getBytes(String str) {
        if (str != null) {
            try {
                return str.getBytes(ENCODE_CHARSET);
            } catch (UnsupportedEncodingException e) {
                return str.getBytes();
            }
        } else {
            return new byte[0];
        }
    }

}
